<?php
General::getTpl()->assign('extraTitle',"Navigations-Kategorien");

if(!isset($_GET['navId']) || !is_numeric($_GET['navId'])) exit;
General::getTpl()->assign('navId',$_GET['navId']);

if(!Admin::checkPermission('canEditNavigation')) {
	General::ouputMessage('Fehler','Sie haben nicht gen&uuml;gend Rechte um diese Seite zu betrachten');
}else{
	
	/**
	 * NEW CATEGORIE
	 */
	if(isset($_GET['do']) && $_GET['do'] == "new") {
		require_once 'kernel/form.php';
		
		// Form-Data
		$formField = array();
		$formField[0]['fieldName'] = "Name";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = "";
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$formField[1]['fieldName'] = "Aktiv?";
		$formField[1]['formName'] = "aktiv";
		$formField[1]['fieldType'] = Form::$TYPE_SELECT;
		$formField[1]['fieldDefaultValue'] = "Ja";
		$formField[1]['fieldCheck'] = false;
		$formField[1]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[1]['fieldSelectMultiple'] = false;
		
		$formField[2]['fieldName'] = "Nur f&uuml;r Admins?";
		$formField[2]['formName'] = "onlyAdmins";
		$formField[2]['fieldType'] = Form::$TYPE_SELECT;
		$formField[2]['fieldDefaultValue'] = "Nein";
		$formField[2]['fieldCheck'] = false;
		$formField[2]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[2]['fieldSelectMultiple'] = false;
		
		
		
		$formField[3]['fieldName'] = "Tab-Einschr&auml;nkung?";
		$formField[3]['formName'] = "tab";
		$formField[3]['fieldDescription'] = "Soll die Kategorie nur angezeigt werden, wenn ein bestimmter Tab angeklickt wurde?";
		$formField[3]['fieldType'] = Form::$TYPE_SELECT;
		$formField[3]['fieldDefaultValue'] = "";
		$formField[3]['fieldCheck'] = false;
		
		$tabs = Navigation::getTabs();
		$keyString = "";
		$valueString = "";
		foreach($tabs as $data) {
			$keyString .= $data['id']."::--::";
			$valueString .= $data['linkName']."::--::";
		}
		$keyString = substr($keyString,0,strlen($keyString)-6);
		$valueString = substr($valueString,0,strlen($valueString)-6);
		
		$formField[3]['fieldSelectOptions'] = "Immer anzeigen::--::Kein Tab::--::".$valueString;
		$formField[3]['fieldSelectKey'] = "::--::-1::--::".$keyString;
		$formField[3]['fieldSelectMultiple'] = true;
		
		$form = new Form();
		$form->initByData("Kategorie erstellen",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			
			// Create Navigation
			$active = ($_POST['aktiv'][0] == "Ja") ? 0:1;
			// Only Admins can see this link?
			$onlyAdmins = ($_POST['onlyAdmins'][0] == "Ja") ? 0:1;
			
			
			if(is_array($_POST['tab'])) $tabs = implode(",",$_POST['tab']);
			else $tabs = "";
			
			Navigation::createCategory($_GET['navId'],$_POST['Name'],$active,$tabs,$onlyAdmins);
			General::ouputMessage('Erfolgreich','Die Kategorie wurde erfolgreich gespeichert.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	

	
	if(isset($_GET['do']) && $_GET['do'] == "edit" && isset($_GET['catId']) && is_numeric($_GET['catId'])) {
		// Edit Navigation?
		require_once 'kernel/form.php';
		
		// Get Data
		$query = DB::getInstance()->query("
			SELECT
				naviId,
				catName,
				catShow,
				catShowTab,
				catOnlyAdmin
			FROM
				".Tables::$naviCatTbl."
			WHERE
				id = '".DB::parseVar($_GET['catId'])."'");
		$data = DB::getInstance()->fetchArray($query);
		
		// Form-Data
		$formField = array();
		$formField[0]['fieldName'] = "Name";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = $data['catName'];
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$formField[1]['fieldName'] = "Aktiv?";
		$formField[1]['formName'] = "aktiv";
		$formField[1]['fieldType'] = Form::$TYPE_SELECT;
		$defaultVal = ($data['catShow'] == 0) ? "Ja":"Nein";
		$formField[1]['fieldDefaultValue'] = $defaultVal;
		$formField[1]['fieldCheck'] = false;
		$formField[1]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[1]['fieldSelectMultiple'] = false;
		
		// Navigation
		$navs = Navigation::getNavigations();
		$selectedNav = "";
		$navData = "";
		foreach($navs as $n) {
			$navData .= $n['naviName']."::--::";
			if($n['id'] == $data['naviId']) $selectedNav = $n['naviName'];
		}
		$navData = substr($navData,0,strlen($navData)-6);
		
		$formField[2]['fieldName'] = "Navigation";
		$formField[2]['formName'] = "navigation";
		$formField[2]['fieldType'] = Form::$TYPE_SELECT;
		$formField[2]['fieldDefaultValue'] = $selectedNav;
		$formField[2]['fieldCheck'] = false;
		$formField[2]['fieldSelectOptions'] = $navData;
		$formField[2]['fieldSelectMultiple'] = false;
		
		$formField[3]['fieldName'] = "Nur f&uuml;r Admins?";
		$formField[3]['formName'] = "onlyAdmins";
		$formField[3]['fieldType'] = Form::$TYPE_SELECT;
		$formField[3]['fieldDefaultValue'] = ($data['catOnlyAdmin'] == 0) ? 'Ja':"Nein";
		$formField[3]['fieldCheck'] = false;
		$formField[3]['fieldSelectOptions'] = "Ja::--::Nein";
		$formField[3]['fieldSelectMultiple'] = false;
		
		
		
		
		
		
		$formField[4]['fieldName'] = "Tab-Einschr&auml;nkung?";
		$formField[4]['formName'] = "tab";
		$formField[4]['fieldDescription'] = "Soll die Kategorie nur angezeigt werden, wenn ein bestimmter Tab angeklickt wurde?";
		$formField[4]['fieldType'] = Form::$TYPE_SELECT;
		$formField[4]['fieldDefaultValue'] = explode(",",$data['catShowTab']);
		$formField[4]['fieldCheck'] = false;
		$tabs = Navigation::getTabs();
		$keyString = "";
		$valueString = "";
		foreach($tabs as $data) {
			$keyString .= $data['id']."::--::";
			$valueString .= $data['linkName']."::--::";
		}
		$keyString = substr($keyString,0,strlen($keyString)-6);
		$valueString = substr($valueString,0,strlen($valueString)-6);
		
		$formField[4]['fieldSelectOptions'] = "Immer anzeigen::--::Kein Tab::--::".$valueString;
		$formField[4]['fieldSelectKey'] = "::--::-1::--::".$keyString;
		$formField[4]['fieldSelectMultiple'] = true;
		
		
		
		
		
		$form = new Form();
		$form->initByData("Kategorie bearbeiten",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			// Save Category
			$active = ($_POST['aktiv'][0] == "Ja") ? 0:1;
			// Only Admins can see this link?
			$onlyAdmins = ($_POST['onlyAdmins'][0] == "Ja") ? 0:1;
			
			// Get Navi-Id
			$selNavi = $_GET['navId'];
			foreach($navs as $id=>$nData) {
				if($nData['naviName'] == $_POST['navigation'][0]) $selNavi = $nData['id'];
			}
			
			if(is_array($_POST['tab'])) $tabs = implode(",",$_POST['tab']);
			else $tabs = "";
			
			Navigation::editCategory($_GET['catId'],$_POST['Name'],$active,$tabs,$selNavi,$onlyAdmins);
			General::ouputMessage('Erfolgreich','Die Navigation wurde erfolgreich bearbeitet.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	

	
	if(isset($_GET['do']) && $_GET['do'] == "del" && isset($_GET['catId']) && is_numeric($_GET['catId'])) {
		require_once './kernel/formCheck.php';
		$form = new FormCheck("Bist du sicher?<br />Die Links der Kategorie werden unwiderruflich gel&ouml;scht.","L&ouml;schen");
		// Done?
		if($form->completeCheck()) {
			if($form->isChecked()) {
				// Delete navigation
				Navigation::deleteCategory($_GET['catId']);
			}
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	

	
	if(isset($_GET['do']) && $_GET['do'] == "up" && isset($_GET['catId']) && is_numeric($_GET['catId'])) {
		$unsortedCats = Navigation::getNavigationCatsByNavId($_GET['navId']);
		// Create Array
		$unsortedArray = array();
		foreach($unsortedCats as $data) {
			$unsortedArray[] = $data['id'];
		}
		
		$sortedArray = General::orderArray($unsortedArray,"up",$_GET['catId']);
		if($sortedArray == false) {
			General::ouputMessage('Fehler','Die Kategorie ist schon ganz oben.','form');
		}else{
			// Save
			foreach($sortedArray as $orderId=>$id) {
				DB::getInstance()->query("
					UPDATE
						".Tables::$naviCatTbl."
					SET
						orderId = '".$orderId."'
					WHERE
						id = '".$id."'");
			}
			General::ouputMessage('Erfolgreich','Die Kategorie wurde erfolgreich verschoben.','form');
		}
	}
	
	if(isset($_GET['do']) && $_GET['do'] == "down" && isset($_GET['catId']) && is_numeric($_GET['catId'])) {
		$unsortedCats = Navigation::getNavigationCatsByNavId($_GET['navId']);
		// Create Array
		$unsortedArray = array();
		foreach($unsortedCats as $data) {
			$unsortedArray[] = $data['id'];
		}
		$sortedArray = General::orderArray($unsortedArray,"down",$_GET['catId']);
	
		if($sortedArray == false) {
			General::ouputMessage('Fehler','Die Kategorie ist schon ganz unten.','form');
		}else{
			// Save
			foreach($sortedArray as $orderId=>$id) {
				DB::getInstance()->query("
					UPDATE
						".Tables::$naviCatTbl."
					SET
						orderId = '".$orderId."'
					WHERE
						id = '".$id."'");
			}
			General::ouputMessage('Erfolgreich','Die Kategorie wurde erfolgreich verschoben.','form');
		}
	}
	
	
	
	$cats = Navigation::getNavigationCatsByNavId($_GET['navId']);
	
	General::getTpl()->assign('cats',$cats);
	
	General::getTpl()->assign('body',
		General::getTpl()->fetch('../admin/navigationCats.html'));
}
?>